﻿namespace SeedObject
{
    using Serializable = System.SerializableAttribute;
    using XmlSerializer = System.Xml.Serialization.XmlSerializer;
    using XmlRoot = System.Xml.Serialization.XmlRootAttribute;
    /// <summary>
    /// Generic type to wrap any type within a seed type
    /// </summary>
    /// <typeparam name="T">Type of the object</typeparam>
    [
         Serializable
        ,XmlRoot("Datum")
    ]
    public class Datum<T> : Seed
    {
        private T _Value;
        /// <summary>
        /// Generic property for a value
        /// </summary>
        public T Value
        {
            get 
            {
                return _Value; 
            }
            set
            {
                SaveAttribute(ID.Value, _Value, 
                    delegate() 
                    {
                        _Value = (T)Attributes[ID.Value]; 
                    }
                );
                _Value = value;
            }

        }
        #region IXmlSerializable Members


            public override void ReadXml(System.Xml.XmlReader reader)
            {
                XmlSerializer Serializer = new XmlSerializer(typeof(T));
                base.ReadXml(reader);
                _Value = (T)Serializer.Deserialize(reader);
                reader.ReadEndElement();
            }

            /// <summary>
            /// Write the contents of the container in xml format
            /// </summary>
            /// <param name="writer">xml writer</param>
            public override void WriteXml(System.Xml.XmlWriter writer)
            {
                base.WriteXml(writer);
                XmlSerializer Serializer = new XmlSerializer(typeof(T));
                Serializer.Serialize(writer, _Value);
            }
        #endregion
    }
}
